import lombok.Setter;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.MethodVisitor;

import java.lang.reflect.Field;

public class MyClassVisitor extends ClassVisitor {

    private static Field mvField;
    @Setter
    private String methodName;
    @Setter
    private String desc;
    @Setter
    private MethodVisitor methodVisitor;

    public MyClassVisitor(int api, ClassVisitor classVisitor) {
        super(api, classVisitor);
    }

    @Override
    public MethodVisitor visitMethod(int access, String name, String _descriptor, String signature, String[] exceptions) {
        MethodVisitor _methodVisitor = super.visitMethod(access, name, _descriptor, signature, exceptions);
        if (name.equals(methodName) && _descriptor.equals(desc)) {
            if (methodVisitor != null) {
                set(_methodVisitor);
            }
            return methodVisitor;
        } else {
            return _methodVisitor;
        }
    }

    private void set(MethodVisitor mv) {

        try {
            mvField.set(this.methodVisitor, mv);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    static {
        try {
            mvField = MethodVisitor.class.getDeclaredField("mv");
            mvField.setAccessible(true);
        } catch (NoSuchFieldException e) {
            throw new RuntimeException(e);
        }
    }
}
